package com.bigdatastudio.common.Utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.http.HttpStatus;

import java.io.Serializable;

/**
 * @author xiao_jie
 * @date 2022/9/12 15:45
 * @describe 返回给前端的通用数据格式类
 */


@Data
@NoArgsConstructor
@AllArgsConstructor
public class R implements Serializable {
    private int code;       //操作结果
    private Object  data;       //返回的数据
    private String  msg;    //返回的操作信息

    //************************* 成功状态   ********************************
    public static R ok(Object data,String msg) {
        R r = new R();
        r.setCode(200);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    // 只有信息没有返回值
    public static R ok(String msg){
        R r = new R();
        r.setMsg(msg);
        r.setCode(200);
        return r;
    }

    //************************* 错误状态   ********************************

    //未知错误
    public static R error() {
        return new R().error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常，请联系管理员");
    }

    //只有错误信息
    public static R error(String msg) {
        return new R().error(HttpStatus.SC_FORBIDDEN,msg);
    }

    //错误码，错误信息
    public R error(int code, String msg) {
        R r = new R();
        r.setMsg(msg);
        r.setCode(code);
        return r;
    }

}
